package ru.ifmo.croak.impl.replication.team1.io;


import ru.ifmo.croak.api.Message;

import java.io.*;
import java.util.UUID;

public class MessageOutputStream implements Closeable {
    private final ObjectOutputStream objectOutputStream;

    public MessageOutputStream(OutputStream outputStream) throws IOException {
        objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(outputStream, MessageInputStream.BUFFER_SIZE));
        objectOutputStream.flush();
    }

    public void writeMessage(Message message) throws IOException {
        objectOutputStream.writeObject(message.getId());
        objectOutputStream.writeLong(message.getTime());
        objectOutputStream.writeUTF(message.getAuthor());
        objectOutputStream.writeUTF(message.getText());
    }

    public void writeInt(int value) throws IOException {
        objectOutputStream.writeInt(value);
    }

    public void writeUUID(UUID uuid) throws IOException {
        objectOutputStream.writeObject(uuid);
    }

    public void writeBoolean(boolean b) throws IOException {
        objectOutputStream.writeBoolean(b);
    }

    public void close() throws IOException {
        objectOutputStream.close();
    }

    public void flush() throws IOException {
        objectOutputStream.flush();
    }

}
